//
//  JWKHomeVisitorView.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/20.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol JWKHomeVisitorViewDelegate : NSObjectProtocol {
    //用户要登录
    func userWillLogin()
    //用户要注册
    func userWillRegister()
    
}

class JWKHomeVisitorView: UIView {

    //创建代理属性
    weak var visitorDelegate : JWKHomeVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        let color : CGFloat = 237/255.0
        
        self.backgroundColor = UIColor.init(red: color, green: color, blue: color, alpha: 1)
        
        addSubview(circleView)
        addSubview(maskImageView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
    }
    

    //开启环形的动画
    private func setupAnimation(){
        //创建basic动画
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.duration = 15
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2*M_PI
        //完成后移除或者切换了视图之后移除
        basic.removedOnCompletion = false
        
        circleView.layer.addAnimation(basic, forKey: nil)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK:-可选项参数可以设默认值,后面的方法调用时可以不传这个参数,取默认值,而且会产生两个重载方法(一个带这个参数,一个不带这个参数) 
    //此方法公开供外部设置不同的访客视图
    func setupUI(tiplabelText:String, imageName:String, isHome:Bool? = false){
    
        if isHome! {
            //如果是首页
            setupAnimation()
        }else{
            //如果不是首页
           largeHouse.hidden = true
           maskImageView.hidden = true
        }
        tipLabel.text = tiplabelText
        circleView.image = UIImage(named: imageName)
        
    }
    
    //MARK:登录按钮点击事件//点击事件--是OC的机制(通知/addTarget)如果加上了private 需要在前面加上@objc
    @objc private func loginBtnClick(){
//       print("登录")
        visitorDelegate?.userWillLogin()
        
    }
    
    //MARK:注册按钮点击事件
    @objc private func registerBtnClick(){
//        print("注册")
        visitorDelegate?.userWillRegister()
    }
    
    //懒加载子控件
    
    //遮盖视图
    lazy var maskImageView : UIImageView = {() -> (UIImageView) in
        let maskImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        
        maskImageView.sizeToFit()
        
        return maskImageView
    }()
    
    lazy var circleView : UIImageView = {() -> (UIImageView) in
        let circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        
        circleView.sizeToFit()
        
        return circleView
    }()
    
    lazy var largeHouse : UIImageView = {() -> (UIImageView) in
        let largeHouse = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        
        largeHouse.sizeToFit()
        
        return largeHouse
    }()
    
    lazy var tipLabel : UILabel = {() -> (UILabel) in
        let tipLabel = UILabel()
        tipLabel.text = "关注一些人,回到这里看看有什么内容"
        tipLabel.font = UIFont.systemFontOfSize(15)
        tipLabel.numberOfLines = 0
        tipLabel.textAlignment = .Center
        //MARK:-限定label的最大宽度的方法
        tipLabel.preferredMaxLayoutWidth = 260
//        或者是
//        tipLabel.snp_makeConstraints { (make) in
//            
//            make.width.equalTo(300)
//            
//        }
        tipLabel.sizeToFit()
        
        return tipLabel
    }()
    
    lazy var loginBtn : UIButton = {() -> (UIButton) in
        let loginBtn = UIButton()
        loginBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        //点击事件--是OC的机制如果加上了private 需要在前面加上@objc
        loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        loginBtn.sizeToFit()
        
        return loginBtn
    }()
    
    lazy var registerBtn : UIButton = {() -> (UIButton) in
        let registerBtn = UIButton()
        let image = UIImage(named:"common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        
        
        registerBtn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        //点击事件--是OC的机制如果加上了private 需要在前面加上@objc
        registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)
        
        registerBtn.sizeToFit()
        
        return registerBtn
    }()
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
        
        //环形视图
        circleView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        //遮盖视图
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(circleView)
        }
        
        //大房子
        largeHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        //MARK:提示文案--就是label
        tipLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            
        }
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
        
//        circleView.center = self.center
//        
//        largeHouse.center = self.center
//        
//        tipLabel.center.x = self.center.x
//        tipLabel.center.y = CGRectGetMaxY(circleView.frame) + 20
//        
//        loginBtn.center.y = CGRectGetMaxY(tipLabel.frame) + 20
//        
//        loginBtn.frame.origin.x = tipLabel.center.x - 20 - loginBtn.frame.size.width
//        
//        registerBtn.center.y = CGRectGetMaxY(tipLabel.frame) + 20
//        
//        registerBtn.frame.origin.x = tipLabel.center.x + 20
    }

}
